(function(){
	$.namespace("com.yogi.wrappers.ebook");

	/**
	 * { 
	 	"type":"e_book",
	 	"catalog":"fashionCatalog",
	 	"div_book":"div_book_id",
	 	"div_next":"div_next_id(optional)",
	 	"div_prev":"div_prev_id(optional)" 
	 }
	 */
	com.yogi.wrappers.ebook.EBookWrapper = function() {
		com.yogi.wrappers.ebook.EBookWrapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.wrappers.ebook.EBookWrapper,com.yogi.core.PartWrapperBase,{	
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.ebook.EBookWrapper.superclass.wrap(part,wrapperConfig,partConfig);		
			
			function adjust_parent_height (ct, partId) {
				var max_height = $(window).height() + "px";
				
				ct.css({
					/*"height" : max_height,*/
					"background-size" : "auto",
					"-webkit-background-size": "auto"
				});
				ct.css("background-repeat","no-repeat");
				var top_ct_id = "flexpart_" + partId;
				var limit = 8;
				while(ct.attr("id") != top_ct_id && ct != null && typeof ct != $(document.body)){
					ct = ct.parent();
					ct.css("height", max_height);
					console.log(ct);
					if(limit -- < 0) break;
				}
			};
			
			function loadEbook(){  //most of the time, the css of ebook across the site are the same
				var outer_ct = $("#" + part.getElementId(wrapperConfig.div_book));
//				console.log("ebook outer_ct=");
//				console.log(outer_ct);
//				console.log(partConfig);
				var w = Math.floor(outer_ct.width() / 2) * 2;
				var h = outer_ct.height();
				
				//console.log("loadEbook:1");
				
				adjust_parent_height(outer_ct, partConfig.Id);
				
				//console.log("loadEbook:2");
				
				var padding = 4;
				var ebook_ct = $("<div id='ebook_" + partConfig.Id + "' style='width:" + w + "px;height:" +h+ "px;margin:" +padding+ "px;padding:0'></div>");
				
				//console.log("loadEbook:3");
				
				outer_ct.append(ebook_ct);
				
				//console.log("loadEbook:4");
				
				var data_js = "config/" + wrapperConfig.catalog + "/json.js";
				//console.log("data_js=" + data_js);
				$.getScript(data_js, function(data,textStatus) {
					if(textStatus != "success") {
						throw "loading script:" + script + " error.";
					}	
					//console.log("images_data=");
					//console.log(images_data);
					for(var i=0;i < images_data.length; i++){
						var ImageUrl = images_data[i].ImageUrl;
						ebook_ct.append("<div style=\"background-image:url(" + ImageUrl + "); width:" + (w/2-padding) + "px;\"></div>");										
					}
					//console.log(ebook_ct.width() + "," + outer_ct.height());
					ebook_ct.turn({width: w - padding * 2, height: h - padding * 2, acceleration: true, shadows: !$.isTouch});
					
					if(wrapperConfig.div_next) {
						$("#" + part.getElementId(wrapperConfig.div_next))
							.click(function() {
								ebook_ct.turn('next');
							});
					}
					if(wrapperConfig.div_prev) {
						$("#" + part.getElementId(wrapperConfig.div_prev))
							.click(function() {
								ebook_ct.turn('previous');
							});
					}									
				});
			}
			
			part.waitForDiv(function(partComp){
					if(typeof($.fn.turn) == "undefined")	{
						var js = "lib/turn.min.js";
						if(typeof Env != 'undefined') {
							js = Env.RootURL + "/yogi/" + js;
						}						
						$.getScript(js, function() {
							com.yogi.core.loadCss(["config/main/" + wrapperConfig.catalog + "/e_book.css"], function() {
								loadEbook();		
							});
						});
					} else {
						loadEbook();
					}
			});
		}
	});
	
	var wrapperManager = window.contentPanel.getWrapperManager();
	wrapperManager.registerWrapperClass("e_book", com.yogi.wrappers.ebook.EBookWrapper);
})();